package com.shujia

import scala.io.{BufferedSource, Source}

object Demo20WordCountScala {
  def main(args: Array[String]): Unit = {
    // 读取words.txt
    val bs: BufferedSource = Source.fromFile("scala/data/words.txt")

    bs.getLines().toList
      // 将每一行的每个单词进行展开
      .flatMap(_.split(","))
      // 按照每个单词进行分组
      .groupBy(word => word)
      // 统计单词的数量
      .map(kv => s"${kv._1},${kv._2.size}")
      // 打印结果
      .foreach(println)

  }

}
